#!/bin/dash
# This is an installation script for auto installing additional support for
# FSC laptops Amilo A16xx
# Written by Assaf Paz (damagedspline@aim.com || damagedspline@gmail.com)

SUCCESS=1
CURRENT_DIR=$(pwd)
KERNEL_VER=$(uname -r)
PACKAGES_UPDATED=FALSE
SCRIPT_VER=0.1
INSTALL_DIR=/usr/local/src

[ "$DEBUG_SCRIPT" = "TRUE" ] && set -x

printMessage()
{
    echo "\033[1m$1\033[0m"
}

checkVersion ()
{
    #check ubuntu version
    if [ -z "$(grep -i Ubuntu /etc/issue)" ]; then
        printMessage "Error: Linux version is unsupported. This script will only work on Ubuntu 7.04 <= ver or Linux Mint Celina"
        return 1
    fi
    return 0
}

#check the arch
checkArch()
{
    return 0
}

#check laptop model
checkModel()
{
    if [ -z "$(grep -i a1650g /var/lib/acpi-support/system-product-name)" -a -z "$(grep -i 1718 /var/lib/acpi-support/system-product-name)" ]; then
        printMessage "Could not detect Amilo A1650G or LI1718."    
        if [ -z "$(dpkg --list acpi-support | grep ^ii)" ]; then
        printMessage "Probably caused because acpi-support is missing. Trying to reinstall..."        
        installPackagesIfNeeded acpi-support
        return checkModel
        else
        printMessage "Error: Sorry, for now this script is only intended for Amilo A1650G && LI1718"
        fi
        return 1
    fi

    return 0
}

#print the welcome message
printWelcome()
{
    printMessage "Welcome to the Fujitsu-Siemens Amilo A16xx automated patch for Ubuntu $LINUX_VER"
    echo ;
    printMessage "This script will auto enable wireless on startup"
    echo ;
    return 0
}

#check superuser
checkSuperUser()
{
    if [ ! $(id -u) = 0 ]; then
		printMessage "Error: To run this script, one must be with root privileges. Rerun this script prefixed by sudo"
		return 1
    fi
    return 0
}

#check that all of the needed prerequisits are met
checkPrerequisits()
{
    checkVersion && \
    checkArch && \
    checkModel && \
    checkSuperUser || return 1
    
    return 0
}

#print predefined error message
printError()
{
	case "$1" in
			setup)
					printMessage "Missing some required files... I you haven't run the setup yet, you should. If you have, you should try remove and run setup again."
					;;
			already)
					printMessage "fscamiloa16xx has already been setup, nothing to do."
					;;
			download)
					printMessage "Error: Unable to download file $2"
					;;
			ndiswrapper)
					printMessage "Error: ndisdriver driver installation failed, setup will continue non-the-less but wireless is not likely to work...;"
		printMessage "Error:...please try to install the driver manually using 'sudo ndiswrapper -i <driver inf file name>'"                 ;;
	esac
	return 0;
}

#install the given packages if needed
installPackagesIfNeeded()
{
	for package in $*; do
			if [ -z "$(dpkg --list $package | grep ^ii)" ]; then
				if [ "$PACKAGES_UPDATED" = "FALSE" ]; then
					printMessage "Updating package lists. This may take a while..."
					if ! apt-get update > /dev/null ; then
					   printMessage "Error: Unable to update package lists... Probably won't be able install packages either..."
						return 1
					fi
							PACKAGES_UPDATED=TRUE
				fi
					printMessage "Installing required package: $package"
				if ! apt-get -qy install $package ; then
					printMessage "Error: Unable to install required package $package"
					return 1 
				fi
			fi
	done
	return 0
}

#add a given line ($1) into a given file ($2)
addLineToFile()
{
    cp $2 $2.part1
    echo $1 > $2.part2
    cat $2.part1 $2.part2 > $2
    rm -f $2.part1 $2.part2
    return 0
}

#blacklist a given module ($1) if not already been blacklisted
blacklistModule()
{
	printMessage "Blacklisting $1"
	if [ -z "$(cat /etc/modprobe.d/blacklist | grep $1)" ]; then
		addLineToFile "blacklist $1" /etc/modprobe.d/blacklist
		if [ -n "lsmod |grep $1" ]; then
			rmmod "$1"
		fi
	fi
	#check for successful blacklisting
	if [ -z "$(cat /etc/modprobe.d/blacklist | grep $1)" ]; then
		printMessage "Error: Blacklisting of $1 failed. You should manually add \"blacklist $1\" to /etc/modprobe.d/blacklist"
		printMessage "Please report this error to damagedspline@google.com"     
	fi
	return 0
}

#unblacklist a given module ($1) if was blacklisted
unblacklistModule()
{
	printMessage "UnBlacklisting $1"
	if [ -z "$(cat /etc/modprobe.d/blacklist | grep $1)" ]; then
	grep -v $1 /etc/modprobe.d/blacklist > blacklist.fsc
	mv blacklist.fsc /etc/modprobe.d/blacklist
	fi
	return 0
}

#do not load a given module ($1) if was previously set to load
unsetLoadModule()
{
	printMessage "Unsetting module load for $1"
	if [ -z "$(cat /etc/options | grep $1)" ]; then
	grep -v $1 /etc/options > options.fsc
	mv options.fsc /etc/options
	fi
	return 0
}

#setup the machine - $1 = setup/check/remove
doSetup()
{
    cd $INSTALL_DIR

	if [ "$1" = "setup" ]; then
		cp $CURRENT_DIR/$0 /etc/init.d 
		if [ -e /etc/rc5.d/S99fsca16xx ]; then
				sudo update-rc.d -f fsca16xx.sh remove              
		fi
		sudo update-rc.d fsca16xx.sh defaults
	fi
		
	if [ ! -d fsca16xx ]; then
		if [ "$1" = "check" ]; then
				printError setup
				return 1
		fi
		mkdir fsca16xx
	fi
	
	if [ "$1" = "setup" ]; then
		cp /etc/modules $INSTALL_DIR/fsca16xx/modules.bak
		cp /etc/modprobe.d/options $INSTALL_DIR/fsca16xx/options.bak
	fi
	
	cd fsca16xx

	if [ "$1" = "remove" ]; then
		rm -f /etc/rc5.d/S99fsca16xx
		sudo update-rc.d -f fsca16xx.sh remove
		rm -f /etc/init.d/fsca16xx.sh
		mv $INSTALL_DIR/fsca16xx/modules.bak /etc/modules 
		mv $INSTALL_DIR/fsca16xx/options.bak /etc/modprobe.d/options
		rm -Rf $INSTALL_DIR/fsca16xx                
		return 0
	fi

	if [ -z "$(grep acer_acpi /etc/modules)" ]; then
		addLineToFile acer_acpi /etc/modules    
	fi
	if [ -z "$(grep acer_acpi /etc/modprobe.d/options)" ]; then
		addLineToFile "options acer_acpi wireless=1" /etc/modprobe.d/options
	else
		if [ "$(grep acer_acpi /etc/modprobe.d/options)" != "options acer_acpi wireless=1" ]; then
			grep -v acer_acpi /etc/modprobe.d/options > options.fsc
			addLineToFile "options acer_acpi wireless=1" options.fsc
			mv options.fsc /etc/modprobe.d/options  
		fi
	fi

	printMessage "Activating wireless"
	if [ -n "$(lsmod |grep acer_acpi)" ]; then
		rmmod acer_acpi
	fi
	modprobe acer_acpi

	printMessage "Done. Wireless should now autostart on boot."
	cd $CURRENT_DIR
	return 0
}

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

if ! checkPrerequisits ; then
        exit 3        
fi

case "$1" in
  start)
        log_daemon_msg "Checking for proper configuration for Fujistu-Siemens laptops"
    if ! doSetup check ; then
                log_end_msg 1        
        fi
        log_end_msg 0
    ;;
  stop)
        #nothing to do
    ;;
  setup) 
        printWelcome
    if ! doSetup setup ; then
                exit 3        
        fi
    # nothing to do
    ;;    
  remove)
        if ! doSetup remove ; then
                exit 3
        fi 
        ;;
  *)
    printMessage "Usage: $0 {start|stop|setup|remove}"
    exit 3
    ;;
esac

:
